package com.weizhu.swufer.core.util;

import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

public class ClassUtil {


    public static String getServiceAddress(String path){
        if (path == null) {
            return null;
        }
        int idx = path.lastIndexOf("/");
        return path.substring(idx+1);
    }

    public static Class<?>[] getInterfacesOfClass(Class<?> clazz){
        Assert.notNull(clazz, "Class must not be null");
        if (clazz.isInterface()) {
            return new Class[]{clazz};
        }
        Set<Class<?>> interfaces = new LinkedHashSet<>();
        Class<?> current = clazz;
        while (current != null) {
            Class<?>[] ifcs = current.getInterfaces();
            interfaces.addAll(Arrays.asList(ifcs));
            current = current.getSuperclass();
        }
        return interfaces.toArray(new Class<?>[0]);
    }

    public static Class<?>[] getParamsType(Object[] args){
        if (args == null) {
            return null;
        }
        Class<?>[] types = new Class[args.length];
        for (int i = 0; i < args.length; i++) {
            Class<?> type = args[i].getClass();
            types[i] = type;
        }
        return types;
    }
}
